package com.jckl.foodathon.sqlite.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.jckl.foodathon.sqlite.model.TB_Brand;

import com.jckl.foodathon.sqlite.model.TB_Store;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table TB__STORE.
*/
public class TB_StoreDao extends AbstractDao<TB_Store, Long> {

    public static final String TABLENAME = "TB__STORE";

    /**
     * Properties of entity TB_Store.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Name = new Property(1, String.class, "name", false, "NAME");
        public final static Property Address_unitNumber = new Property(2, String.class, "address_unitNumber", false, "ADDRESS_UNIT_NUMBER");
        public final static Property Address_buildingName = new Property(3, String.class, "address_buildingName", false, "ADDRESS_BUILDING_NAME");
        public final static Property Address_streetName = new Property(4, String.class, "address_streetName", false, "ADDRESS_STREET_NAME");
        public final static Property Address_area = new Property(5, String.class, "address_area", false, "ADDRESS_AREA");
        public final static Property Address_postcode = new Property(6, String.class, "address_postcode", false, "ADDRESS_POSTCODE");
        public final static Property Address_state = new Property(7, String.class, "address_state", false, "ADDRESS_STATE");
        public final static Property Address_country = new Property(8, String.class, "address_country", false, "ADDRESS_COUNTRY");
        public final static Property Longitude = new Property(9, Double.class, "longitude", false, "LONGITUDE");
        public final static Property Latitude = new Property(10, Double.class, "latitude", false, "LATITUDE");
        public final static Property ContactNo = new Property(11, String.class, "contactNo", false, "CONTACT_NO");
        public final static Property ContactPerson = new Property(12, String.class, "contactPerson", false, "CONTACT_PERSON");
        public final static Property Notes = new Property(13, String.class, "notes", false, "NOTES");
        public final static Property Image = new Property(14, byte[].class, "image", false, "IMAGE");
        public final static Property Website = new Property(15, String.class, "website", false, "WEBSITE");
        public final static Property Email = new Property(16, String.class, "email", false, "EMAIL");
        public final static Property BrandId = new Property(17, Long.class, "brandId", false, "BRAND_ID");
    };

    private DaoSession daoSession;

    private Query<TB_Store> tB_Brand_StoresQuery;

    public TB_StoreDao(DaoConfig config) {
        super(config);
    }
    
    public TB_StoreDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'TB__STORE' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'NAME' TEXT NOT NULL ," + // 1: name
                "'ADDRESS_UNIT_NUMBER' TEXT," + // 2: address_unitNumber
                "'ADDRESS_BUILDING_NAME' TEXT," + // 3: address_buildingName
                "'ADDRESS_STREET_NAME' TEXT," + // 4: address_streetName
                "'ADDRESS_AREA' TEXT," + // 5: address_area
                "'ADDRESS_POSTCODE' TEXT," + // 6: address_postcode
                "'ADDRESS_STATE' TEXT," + // 7: address_state
                "'ADDRESS_COUNTRY' TEXT," + // 8: address_country
                "'LONGITUDE' REAL," + // 9: longitude
                "'LATITUDE' REAL," + // 10: latitude
                "'CONTACT_NO' TEXT," + // 11: contactNo
                "'CONTACT_PERSON' TEXT," + // 12: contactPerson
                "'NOTES' TEXT," + // 13: notes
                "'IMAGE' BLOB," + // 14: image
                "'WEBSITE' TEXT," + // 15: website
                "'EMAIL' TEXT," + // 16: email
                "'BRAND_ID' INTEGER);"); // 17: brandId
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'TB__STORE'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, TB_Store entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getName());
 
        String address_unitNumber = entity.getAddress_unitNumber();
        if (address_unitNumber != null) {
            stmt.bindString(3, address_unitNumber);
        }
 
        String address_buildingName = entity.getAddress_buildingName();
        if (address_buildingName != null) {
            stmt.bindString(4, address_buildingName);
        }
 
        String address_streetName = entity.getAddress_streetName();
        if (address_streetName != null) {
            stmt.bindString(5, address_streetName);
        }
 
        String address_area = entity.getAddress_area();
        if (address_area != null) {
            stmt.bindString(6, address_area);
        }
 
        String address_postcode = entity.getAddress_postcode();
        if (address_postcode != null) {
            stmt.bindString(7, address_postcode);
        }
 
        String address_state = entity.getAddress_state();
        if (address_state != null) {
            stmt.bindString(8, address_state);
        }
 
        String address_country = entity.getAddress_country();
        if (address_country != null) {
            stmt.bindString(9, address_country);
        }
 
        Double longitude = entity.getLongitude();
        if (longitude != null) {
            stmt.bindDouble(10, longitude);
        }
 
        Double latitude = entity.getLatitude();
        if (latitude != null) {
            stmt.bindDouble(11, latitude);
        }
 
        String contactNo = entity.getContactNo();
        if (contactNo != null) {
            stmt.bindString(12, contactNo);
        }
 
        String contactPerson = entity.getContactPerson();
        if (contactPerson != null) {
            stmt.bindString(13, contactPerson);
        }
 
        String notes = entity.getNotes();
        if (notes != null) {
            stmt.bindString(14, notes);
        }
 
        byte[] image = entity.getImage();
        if (image != null) {
            stmt.bindBlob(15, image);
        }
 
        String website = entity.getWebsite();
        if (website != null) {
            stmt.bindString(16, website);
        }
 
        String email = entity.getEmail();
        if (email != null) {
            stmt.bindString(17, email);
        }
 
        Long brandId = entity.getBrandId();
        if (brandId != null) {
            stmt.bindLong(18, brandId);
        }
    }

    @Override
    protected void attachEntity(TB_Store entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public TB_Store readEntity(Cursor cursor, int offset) {
        TB_Store entity = new TB_Store( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getString(offset + 1), // name
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // address_unitNumber
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // address_buildingName
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // address_streetName
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // address_area
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // address_postcode
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // address_state
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // address_country
            cursor.isNull(offset + 9) ? null : cursor.getDouble(offset + 9), // longitude
            cursor.isNull(offset + 10) ? null : cursor.getDouble(offset + 10), // latitude
            cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // contactNo
            cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // contactPerson
            cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13), // notes
            cursor.isNull(offset + 14) ? null : cursor.getBlob(offset + 14), // image
            cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15), // website
            cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16), // email
            cursor.isNull(offset + 17) ? null : cursor.getLong(offset + 17) // brandId
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, TB_Store entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setName(cursor.getString(offset + 1));
        entity.setAddress_unitNumber(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setAddress_buildingName(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setAddress_streetName(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setAddress_area(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setAddress_postcode(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setAddress_state(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setAddress_country(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setLongitude(cursor.isNull(offset + 9) ? null : cursor.getDouble(offset + 9));
        entity.setLatitude(cursor.isNull(offset + 10) ? null : cursor.getDouble(offset + 10));
        entity.setContactNo(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setContactPerson(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setNotes(cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
        entity.setImage(cursor.isNull(offset + 14) ? null : cursor.getBlob(offset + 14));
        entity.setWebsite(cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15));
        entity.setEmail(cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16));
        entity.setBrandId(cursor.isNull(offset + 17) ? null : cursor.getLong(offset + 17));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(TB_Store entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(TB_Store entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "stores" to-many relationship of TB_Brand. */
    public List<TB_Store> _queryTB_Brand_Stores(Long brandId) {
        synchronized (this) {
            if (tB_Brand_StoresQuery == null) {
                QueryBuilder<TB_Store> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.BrandId.eq(null));
                tB_Brand_StoresQuery = queryBuilder.build();
            }
        }
        Query<TB_Store> query = tB_Brand_StoresQuery.forCurrentThread();
        query.setParameter(0, brandId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getTB_BrandDao().getAllColumns());
            builder.append(" FROM TB__STORE T");
            builder.append(" LEFT JOIN TB__BRAND T0 ON T.'BRAND_ID'=T0.'_id'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected TB_Store loadCurrentDeep(Cursor cursor, boolean lock) {
        TB_Store entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        TB_Brand tB_Brand = loadCurrentOther(daoSession.getTB_BrandDao(), cursor, offset);
        entity.setTB_Brand(tB_Brand);

        return entity;    
    }

    public TB_Store loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<TB_Store> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<TB_Store> list = new ArrayList<TB_Store>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<TB_Store> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<TB_Store> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
